﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransformVector : MonoBehaviour {

    private static TransformVector thas;

    private Transform look;
    public GameObject node;
    public int lookId;

    bool isShow = false;

    public enum Unit
    {
        no = 0,
        x = 1,
        y = 2,
        z = 3
    }
    Camera mainCamera;
    public static TransformVectorUnit selectUnit;

    /// <summary>
    /// x 1 , y 2 , z 3
    /// </summary>
    public Unit unit = Unit.no;

    public static TransformVector Thas
    {
        get
        {
            if (thas == null)
            {
                thas = GameObject.FindObjectOfType<TransformVector>();
            }
            return thas;
        }
    }

    public Transform Look
    {
        get
        {
            return look;
        }

        set
        {
            if (TransformVector.selectUnit)
            {
                TransformVector.selectUnit.NoSelect();
            }
            if (value)
            {
                this.gameObject.SetActive(true);
                var transformUnitComponent = value.GetComponent<TransformUnitComponent>();
                if (transformUnitComponent)
                {
                    transformUnitComponent.UiComponent.LookThis();
                    //UIItemTransformUnitComponent.UnLookThis();
                    UIComponentMastermind.instance.ShowRightUITransform(); 
                    UIComponentMastermind.instance.ShowLeftUITransform(); 
                }
            }
            else
            {
                this.gameObject.SetActive(false);
                UIItemTransformUnitComponent.UnLookThis();
            }
            look = value;
        }
    }

    void Awake()
    {
        mainCamera = Camera.main;
        if (thas == null)
        {
            thas = this;
        }
    }

    void Start() {
        Look = null;
    }
	
	void LateUpdate() {

        if (Look)
        {
            if (isShow)
            {
                if (Look.GetInstanceID() == lookId)
                {

                }
                else
                {
                    this.transform.position = Look.position;
                    lookId = Look.GetInstanceID();
                }
            }
            else
            {
                this.transform.position = Look.position;
                lookId = Look.GetInstanceID();
                isShow = true;
            }
            Look.position = this.transform.position;
        }
        else
        {
            isShow = false;
        }
        var va = mainCamera.WorldToScreenPoint(this.transform.position);
        var vb = mainCamera.WorldToScreenPoint((this.transform.position + mainCamera.transform.rotation * new Vector3(0, 13, 0)));
        this.transform.localScale = new Vector3(1, 1, 1) * (Screen.height / Vector3.Distance(va, vb)) / 5f;
    }
    private void OnDestroy()
    {
        selectUnit = null;
    }
}
